<?php
namespace Admin\Controller;

class DatadictController extends AbstractController  {
	
	protected $Form;

	public function _initialize(){
		$this->Form = D('Datadict');
    }
	
    public function index(){
        $this->display();
    }
	
	public function dicttype(){
		$res = $this->Form->MyshowList(0);
		$this->assign("list",$res['list']);
    	$this->assign("page",$res['page']);
		$this->display();
	}
	
	public function datadict(){
		$res = $this->Form->MyshowList(1);
		$this->assign("list",$res['list']);
    	$this->assign("page",$res['page']);
		$this->display();
	}
	
	public function save($type = false, $id = 0){
		if($type != false){
			if($type == "add"){
				$result = $this->Form->Myadd();
			}else if($type == "edit"){
				if(I('id')){
					$res = $this->Form->Myshow(array("ID" => I('id'), "ParentID" => 0));
					$this->assign("data",$res[0]);
				}
				$this->display();
			}else if($type == "update"){
				$this->Form->_validate = array(
					array('Name','require','请输入类型名称'),
					array('ID','require','ID不能为空')
				);
				$result = $this->Form->Myupdata();
			}else if($type == "delete"){
				$sql = " UPDATE datadict SET IsDel = 1 WHERE ID = {$id}";
				$res = $this->Form->execute($sql);
				if($res){
					$this->success('字典类型删除！');
				}else{
					$this->error('字典类型删除！');
				}		
			}
			if($result['state'] == 1 && !empty($result)){
				$this->success($result['msg']);
			}else if($result['state'] == 0 && !empty($result)){
				$this->error($result['msg']);
			}
		}else{
			$this->display();
		}
	}
	
	public function savea($type = false, $id = 0){
		if($type != false){
			if($type == "add"){
				$result = $this->Form->Myadd();
			}else if($type == "edit"){
				if(I('id')){
					$res = $this->Form->Myshow(array("ID" => I('id')));
					$this->assign("data",$res[0]);
					$res = $this->Form->Myshow(array("ParentID" => 0, "IsDel" => 0));
					$this->assign("selectdata",$res);
				}
				$this->display();
			}else if($type == "update"){
				$this->Form->_validate = array(
					array('Name','require','请输入类型名称'),
					array('ID','require','ID不能为空'),
					array('ParentID','require','ParentID不能为空')
				);
				$result = $this->Form->Myupdata();
			}else if($type == "delete"){
				$sql = " UPDATE datadict SET IsDel = 1 WHERE ID = {$id}";
				$res = $this->Form->execute($sql);
				if($res){
					$this->success('数据字典删除！');
				}else{
					$this->error('数据字典删除！');
				}		
			}
			if($result['state'] == 1 && !empty($result)){
				$this->success($result['msg']);
			}else if($result['state'] == 0 && !empty($result)){
				$this->error($result['msg']);
			}
		}else{
			$res = $this->Form->Myshow(array("ParentID" => 0, "IsDel" => 0));
			$this->assign("selectdata",$res);
			$this->display();
		}
	}
	
}